package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.OfficeDomain;
import com.xingkeduo.pojo.json.OfficeJson;
import com.xingkeduo.pojo.query.OfficeQuery;
import com.xingkeduo.pojo.table.OfficeTable;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;


/**
 * 子公司管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/office")
public class OfficeController extends InitController {

    @Autowired
    private OfficeDomain officeDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            OfficeQuery officeQuery = OfficeJson.createQueryForOffice(urlParam);
            Long id = officeQuery.getId();
            OfficeTable officeTable = officeDomain.getOfficeInfoById(id);
            return returnUtil.getSuccess(OfficeJson.toJson(officeTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid OfficeTable officeVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            officeVo.setCreatedBy(this.getOperator(request));
            log.info("添加分公司接口请求参数:{}", JSON.toJSONString(officeVo));
            int office = officeDomain.createOffice(officeVo);
            log.info("添加分公司接口返回值:{}", JSON.toJSONString(office));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid OfficeTable officeVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);

            OfficeTable officeTable = OfficeJson.createTableForModifyOffice(urlParam,officeVo);
            officeTable.setUpdatedBy(this.getOperator(request));
            log.info("修改分公司接口请求参数:{}", JSON.toJSONString(officeVo));
            int i = officeDomain.modifyOffice(officeTable);
            log.info("修改分公司接口返回值:{}", JSON.toJSONString(i));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            OfficeQuery officeQuery = OfficeJson.createQueryForOffice(urlParam);
            long id = officeQuery.getId();
            officeDomain.delOffice(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            OfficeQuery officeQuery = new OfficeQuery();
            if(!CommonUtil.isEmpty(urlParam)){
                officeQuery =  JSON.parseObject(urlParam,OfficeQuery.class);
            }
            List<OfficeTable> officeList= officeDomain.getOfficeList(officeQuery);

//集合      Set<Long> collect = managerTablePage.getDtoList().stream().map(ManagerTable::getDeptmentId).collect(Collectors.toSet());
//拷贝      BeanUtils.copyProperties(order, orderDetailResponseDto);

            return returnUtil.getSuccess(OfficeJson.toJson(officeList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
